package cn.hj.user.controller;

import cn.hj.basic.dto.BinderDto;
import cn.hj.basic.dto.LoginDto;
import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.util.AjaxResult;
import cn.hj.user.domain.WxUser;
import cn.hj.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 账号密码登录
     * @param dto
     * @return
     */
    @PostMapping("/acount")
    public AjaxResult acountLogin(@RequestBody LoginDto dto){
        try {
            //service层需要返回一个token和logininfo，这里使用map接受
            Map<String,Object> map = logininfoService.acountLogin(dto);
            return AjaxResult.newAjaxResult().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage("系统错误，请稍后重试");
        }
    }

    /**
     * 微信登录接口
     * @param param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> param){
        try {
            return logininfoService.wechat(param.get("code"));
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage("系统错误，请稍后重试");
        }
    }
    /**
     * 微信绑定接口
     * @param dto
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody BinderDto dto){
        try {
            Map<String,Object> map = logininfoService.binderWechat(dto);
            return AjaxResult.newAjaxResult().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage("系统错误，请稍后重试");
        }
    }
}
